<?php
class Print_colours_model extends MY_Model{
    
    function Print_colours_model(){
        parent::MY_Model();
        $this->bur_papercolours='bur_papercolours';
        $this->load->database();
    }

    	function total_print_colours() {
		$this->db->from($this->bur_papercolours);
	
		return $this->db->count_all_results();
	} 
	
    function setdefault($id)
	{
		//$arr = array('papercolour_default' =>1);
		$this->db->set('papercolour_default','0');
        //$this->db->where('papercolour_id',$id);
        $this->db->update($this->bur_papercolours);
		
		$this->db->set('papercolour_default','1');
        $this->db->where('papercolour_id',$id);
        $this->db->update($this->bur_papercolours);
		//echo $this->db->last_query();die();
		
	}
	
    function moveup($paper_colour_id)
    {
      //  echo $paper_colour_id;
    $this->db->select('papercolour_order');  
    $this->db->where('papercolour_id',$paper_colour_id);
	 	$query = $this->db->get($this->bur_papercolours);
		if($query->num_rows() > 0){
        $order=  $query->row_array();
        $first_order= $order['papercolour_order'];
        $first_id=$paper_colour_id;
        }
  
         $query = $this->db->query("select papercolour_order,papercolour_id from bur_papercolours where papercolour_order >'$first_order' order by papercolour_order asc");

if ($query->num_rows() > 0)
{
   $row = $query->row_array();
   $second_order= $row['papercolour_order'];
   $second_id=$row['papercolour_id'];
             echo $sql="update bur_papercolours set papercolour_order='$first_order' where papercolour_id='$second_id'";
            $query=$this->db->query($sql);
    $sql="update bur_papercolours set papercolour_order='$second_order' where papercolour_id='$first_id'";
         $query=$this->db->query($sql);
// $this->db->last_query();
} 
    }   
    function list_print_colours($limit='false',$offset='false'){
    	if ($limit){
        	$limit = (int)$limit;
    		$offset = (int)$offset;
        }
		
		$this->db->order_by('papercolour_order','DESC');
      

		if ($limit){
            $query = $this->db->get($this->bur_papercolours,$limit,$offset);
        }else{
            $query = $this->db->get($this->bur_papercolours);
        }
    // $query=$this->db->get($this->bur_papersizes); 
    
     if($query->num_rows()>0){
        return $query->result_array();
        
     }else{
        return false;
        
     }
    }

    function get_print_colours_detail($paper_colour_id=''){
		
		$this->db->where('papercolour_id',$paper_colour_id);
	 	$query = $this->db->get($this->bur_papercolours);
		if($query->num_rows() > 0){
			return $query->row_array();
		}else{
			return FALSE;
		}
	}  
     function delete_print_colours($paper_colour_id ){
        if(!empty($paper_colour_id)){
            foreach($paper_colour_id as $id):
            $this->db->where('papercolour_id',$id);
            $this->db->delete($this->bur_papercolours);
            endforeach;
            
        } else {
            return False;
            
        }  
        }
    function do_action(){
        
        if($this->input->post('papercolour_id'))
        {
          
            
            $this->db->set('papercolour_title',$this->input->post('papercolour_title'));
            $this->db->where('papercolour_id',$this->input->post('papercolour_id'));
            $this->db->update($this->bur_papercolours);
            
       }else{
          
		  $this->db->select_max('papercolour_order');
		  $query=$this->db->get($this->bur_papercolours);
		  if($query->num_rows() > 0){
		
			 $row= $query->row_array();
			
			 $new_order= $row['papercolour_order']+1;
			 $this->db->set('papercolour_title',$this->input->post('papercolour_title'));
		  $this->db->set('papercolour_order',$new_order);
          $this->db->insert($this->bur_papercolours);
			
		}else{
		return FALSE;
		}
          
            
      }
      
    }
}
?>
